<?php

namespace App\Http\Controllers\Admin;
use Image;
use App\News;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateNewsRequest;
use Illuminate\Support\Facades\Storage;
class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $news=News::orderBy('created_at','Desc')->paginate(10);
        return view('admin.news.lists',compact('news'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.news.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateNewsRequest $request)
    {

        $news = new News;
        $news->title = $request->title;
        $news->keywords = $request->keywords;
        $news->desc = $request->desc;
        $news->remark = $request->remark;
        $news->content = $request->contents;
        $news->views = $request->views;
        $news->pic = $this->thumbil($request);
        $blur= $news->save();
        if($blur){
            session()->flash("data",['class'=>'success','msg'=>"添加新闻成功"]);
        }else{
            session()->flash("data",['class'=>'danger','msg'=>"添加新闻失败"]);
        }
    }
    /**
     * 图片上传处理
     */
    public function thumbil($request){
        if ($request->hasFile('pic')) {
            $thumb=$request->pic;
            $name=$thumb->hashName();
            $thumb->storeAs('/public/thumb/',$name);
            $path=storage_path('app/public/uploads/'.$name);
            Image::make($thumb)->resize(200,90)->save($path);
            return $name;



        }

    }
    /**
     * Display the specified resource.
     *
     * @param  \App\news  $news
     * @return \Illuminate\Http\Response
     */
    public function show(news $news)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\news  $news
     * @return \Illuminate\Http\Response
     */
    public function edit(news $news)
    {

        return view('admin.news.edit')->with('news',$news);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\news  $news
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, news $news)
    {
        $data=[
            'title' => $request->title,
            'keywords' =>$request->keywords,
            'desc' => $request->desc,
            'remark' => $request->remark,
            'content' => $request->contents,
            'views' => $request->views,

        ];
        if ($request->hasFile('pic')) {
            $this->exitsimg($news->pic);
            $data['pic'] =$this->thumbil($request);
        }
        $model=new News;
        $blur=$model::where('id',$news->id)->update($data);
        if($blur){
            return redirect()->route('admin.news.index');
        }else{
            return back();
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\news  $news
     * @return \Illuminate\Http\Response
     */
    public function destroy(news $news)
    {
        $model=new News;
        $blur=$model::where('id',$news->id)->delete();
        $this->exitsimg($news->pic);
        if($blur){
            return redirect()->route('admin.news.index');
        }
    }
    // 检查文件是否存在
    public function exitsimg($img){
        $path='public/uploads/'.$img;
        $exists = Storage::disk('local')->exists($path);
        if ($exists) {
            $this->delimg($path);
        }
    }
    // 删除图片
    public function delimg($path){
        $result=Storage::delete($path);
        if ($result) {
            return true;
        }else {
            return false;
        }
    }
}
